using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeLancer.HRB.Core.Web.Controls;
using FreeLancer.HRB.Domains;
using FreeLancer.HRB.Services;
using FreeLancer.HRB.Core;

namespace FreeLancer.HRB.WebApp
{
  public partial class Blog : WebBasePage
  {
    private const string BlogId = "183bfcf8-abfe-40b2-9b7b-784d8fa84ea0";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSend_OnClick(object sender, EventArgs e)
    {
      Posts post = GetPost();
      bool flag = true;
      if (post != null)
      {
        using (ConnectionScope.CreateTransaction())
        {
          GlobalCache.PostServiceInstance.Save(post);
          PostCategory postCategory = new PostCategory();
          postCategory.CategoryId = new Guid(BlogId);
          postCategory.PostId = post.PostId;
          postCategory.LanguageId = _languageId;

          new PostCategoryService().Save(postCategory);

          flag = ConnectionScope.Complete();
          if (!flag)
            lblErrorUpdateFailed.Visible = true;
        }

        if (flag)
          Response.Redirect("~/Category.aspx?id=" + BlogId);
      }
    }

    private Posts GetPost()
    {
      Posts post = null;
      if (!(string.IsNullOrEmpty(ASPxHtmlEditor.Html) || string.IsNullOrEmpty(txtName.Value)))
      {
        DateTime now = DateTime.Now;
        post = new Posts();
        post.Author = txtName.Value;
        post.Address = txtAddress.Value;
        post.Email = txtEmail.Value;
        post.Phone = txtPhone.Value;
        post.Title = txtTitle.Value;
        post.IsPublished = true;
        post.DateCreated = now;
        post.DateModified = now;
        post.PostContent = ASPxHtmlEditor.Html;
        post.Description = string.Empty;
        post.ImagePath = null;
        post.PostId = Guid.NewGuid();
        post.IsCommentAllowed = true;
      }

      return post;
    }
  }
}
